package com.electric.util;

import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;

/**
 * @author Kevin Zhang
 *
 * 2011-5-10
 */
public class CookieUtil {
	public static void addCookie(String name, String value) {
		// 创建Cookie
		Cookie cookie = new Cookie(name, URLEncoder.encode(value));
		// 设置Cookie的生命周期
		cookie.setMaxAge(60 * 60 * 24 * 365);
		ServletActionContext.getResponse().addCookie(cookie);
	}

	public static void removeCookie(String name) {
		addCookie(name, "");
	}

	public static String getCookieValue(Cookie[] cookies, String str) {
		String result = null;
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals(str)) {
				result = cookie.getValue();
				break;
			}
		}
		return result;
	}
}
